package blackhouse_servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import blackhouse_entity.student;
import blackhouse_server.seeclassServer;
import blackhouse_server.studentinfo_server;

import com.alibaba.fastjson.JSON;

@WebServlet("/studentinfo")
public class studentinfo_servlet extends HttpServlet
{

	seeclassServer	server1	= new seeclassServer();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String code = req.getParameter("code");
		String ID = (String) req.getSession().getAttribute("user");
		studentinfo_server stu = new studentinfo_server();
		PrintWriter print = resp.getWriter();
		List<student> list = null;
		String json = null;
		try
		{
			if (code.equals("2"))
			{
				int stuage = Integer.parseInt(req.getParameter("stuAge"));
				String stuqq = req.getParameter("stuQQ");
				String stuphone = req.getParameter("stuPhone");
				String stuClassid = req.getParameter("stuclassid");
				String stuaddress = req.getParameter("stuAddress");
				String stucode = null;
				boolean num = false;
				if (req.getParameter("stucode") != null
						&& !req.getParameter("stucode").equals(""))
				{
					stucode = req.getParameter("stucode");
					num = stu.changeinfo_server(stuage, stuqq, stuphone,
							stuaddress, stuClassid, stucode);
				} else
				{
					num = stu.changeinfo_server(stuage, stuqq, stuphone,
							stuaddress, stuClassid, ID);
				}
				print.write(num + "");
			} else if (code.equals("3"))
			{
				String stuname = req.getParameter("stuName");
				String stusex = req.getParameter("stuSex");
				int stuage = Integer.parseInt(req.getParameter("stuAge"));
				String stuqq = req.getParameter("stuQQ");
				String stuphone = req.getParameter("stuPhone");
				int stuClassid = Integer.parseInt(req
						.getParameter("stuclassid"));
				String stuaddress = req.getParameter("stuAddress");
				boolean isok = stu.insertstuinfo(stuname, stusex, stuage,
						stuqq, stuphone, stuaddress, stuClassid);
				print.write(isok + "");
			} else if (code.equals("1"))
			{
				String key = req.getParameter("key");
				boolean num = stu.delstu(key);
				print.write(num + "");

			} else if (code.equals("10"))
			{
				String key = req.getParameter("key");
				json = JSON.toJSONString(stu.studentinfo_server(key, code));
				print.write(json);
			} else
			{
				json = JSON.toJSONString(stu.studentinfo_server(ID, code));
				print.write(json);
			}

		} catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
}
